package com.ruoyi.system.mc.annotations;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mc.entity.McLog;
import com.ruoyi.system.mc.service.McLogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;

/*
 *  @author 雨安
 *  类名： LogAopAction
 *  创建时间：2024/5/20
 */
@Component
@Aspect
@Slf4j
public class LogAopAction {

    @Resource
    private McLogService mcLogService;

    //操作日志记录
    @Before("@annotation(mcLogAn)")
    public void logMethodExecution(JoinPoint joinPoint, McLogAn mcLogAn) {
        //创建日志对象
        McLog mcLog = new McLog();
        //设置请求内容
        mcLog.setLogContent(mcLogAn.logContent());
        //设置请求人
        mcLog.setLogDoUser(SecurityUtils.getUsername());
        //设置请求类型
        mcLog.setBusinessType(mcLogAn.businessType());
        //设置请求方法
        mcLog.setRequestMethod(mcLogAn.requestMethod());
        // 获取当前的日期和时间
        LocalDateTime now = LocalDateTime.now();
        //设置默认格式
        mcLog.setCreateBy(SecurityUtils.getUsername());
        mcLog.setUpdateBy(SecurityUtils.getUsername());
        mcLog.setCreateTime(now);
        mcLog.setUpdateTime(now);
        mcLog.setDeleteFlag(Boolean.FALSE);
        //执行添加
        mcLogService.save(mcLog);
    }


}
